<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>知识库管理<small>饮食库管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>知识库管理</el-breadcrumb-item>
                    <el-breadcrumb-item>饮食库管理</el-breadcrumb-item>
                </el-breadcrumb>
                <el-card class="box-card">
                    <div class="boxMain">
                        <el-button style="margin-bottom: 20px;margin-right: 20px" type="primary"
                                   @click="downloadTemplate()">模板下载
                        </el-button>
                        <el-upload action="/food/upload.do"
                                   name="excelFile"
                                   :show-file-list="false"
                                   :on-success="handleSuccess"
                                   :before-upload="beforeUpload">
                            <el-button type="primary">上传文件</el-button>
                        </el-upload>
                    </div>
                </el-card>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="膳食助记码/项目名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                        <el-button @click="findByCondition()" class="dalfBut">查询</el-button>
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="foodName" label="膳食名称" align="center"></el-table-column>
                        <el-table-column prop="foodTip" label="膳食说明" align="center"></el-table-column>
                        <el-table-column prop="foodUnit" label="食用尺寸" align="center"></el-table-column>
                        <el-table-column prop="suitableAge" label="适用年龄" align="center"></el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="饮食项" :visible.sync="dialogFormVisible">
                            <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="膳食名称" prop="foodName">
                                            <el-input v-model="formData.foodName"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="膳食说明" prop="foodTip">
                                            <el-input v-model="formData.foodTip"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="食用尺寸" prop="foodUnit">
                                            <el-input v-model="formData.foodUnit"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="适用年龄" prop="suitableAge">
                                            <el-input v-model="formData.suitableAge"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="注意事项">
                                            <el-input v-model="formData.attention" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                pagination: {//分页相关模型数据
					  currentPage: 1,//当前页码
					  pageSize:10,//每页显示的记录数
					  total:0,//总记录数
					  queryString:null//查询条件
				},
				dataList: [],//当前页要展示的分页列表数据
                formData: {},//表单数据
                dialogFormVisible: false,//增加表单是否可见
                rules: {//校验规则
                    suitableAge: [{ required: true, message: '使用年龄为必填项', trigger: 'blur' }],
                    foodUnit: [{ required: true, message: '使用尺寸为必填项', trigger: 'blur' }],
                    foodTip: [{ required: true, message: '膳食说明为必填项', trigger: 'blur' }],
                    foodName: [{ required: true, message: '膳食名称为必填项', trigger: 'blur' }]
                }
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                this.findPage();//VUE对象初始化完成后调用分页查询方法，完成分页查询
            },
            methods: {
                //上传之前进行文件格式校验
                beforeUpload(file) {
                    return true;
                },
                //下载模板文件
                downloadTemplate() {
                    location.href = "../template/food.xls";
                },
                //上传成功提示
                handleSuccess(response, file) {
                    if (response.flag) {
                        this.findPage();
                        this.$message({
                            message: response.message,
                            type: 'success'
                        });
                        this.initData(null);
                    } else {
                        this.$message.error(response.message);
                    }
                    this.findPage();
                },
                //查询分页
                findByCondition(){
                    this.pagination.currentPage=1;
                    this.findPage();
                },
                //分页查询
                findPage() {
                    //发送ajax请求，提交分页相关请求参数（页码、每页显示记录数、查询条件）
                    let param = {
                        currentPage:this.pagination.currentPage,
                        pageSize:this.pagination.pageSize,
                        queryString: this.pagination.queryString
                    };
                    axios.post("/food/findPage.do",param).then((res)=>{
                        //解析Controller响应回的数据，为模型数据赋值
                        this.pagination.total = res.data.total;
                        this.dataList = res.data.rows;
                    });
                },
                //执行查询框的分页
                findByCondition(){
                    this.pagination.currentPage=1;
                    this.findPage();
                },
                // 重置表单
                resetForm() {
                    this.formData = {};//重置数据
                    if (this.$refs['dataAddForm']!==undefined) {
                        this.$refs['dataAddForm'].clearValidate();
                    }
                },
                //切换页码
                handleCurrentChange(currentPage) {
                    //设置最新的页码
                    this.pagination.currentPage = currentPage;
                    //重新调用findPage方法进行分页查询
                    this.findPage();
                },
                //显示添加窗口
                handleCreate() {
                    this.dialogFormVisible = true;
                    this.resetForm();
                },
                //添加
                handleAdd () {
                    //进行表单校验
                    this.$refs['dataAddForm'].validate((valid) => {
                        if(valid){
                            //表单校验通过，发生ajax请求，将录入的数据提交到后台进行处理
                            console.log(this.formData);
                            axios.post("/food/handleAdd.do",this.formData).then((res) => {
                                //关闭新增窗口
                                this.dialogFormVisible = false;
                                if(res.data.flag){//执行成功
                                    //新增成功后，重新调用分页查询方法，查询出最新的数据
                                    this.findPage();
                                    //弹出提示信息
                                    this.$message({
                                        message:res.data.message,
                                        type:'success'
                                    });
                                }else{//执行失败
                                    //弹出提示
                                    this.$message.error(res.data.message);
                                }
                            }).catch((resp)=>{
                                if (resp == 'Error:Request failed with status code 403'){
                                    this.$message.error('无访问权限');
                                    return;
                                }else {
                                    this.$message.error('发送了未知的错误请重试');
                                    return;
                                }
                            });
                        }
                    });
                },
            }
        })
    </script>
</html>